package com.freedy.concurrent.javaThread.daemon;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;


/**
 * 守护线程
 * @author Freedy
 * @date 2021/4/20 18:25
 */
@Slf4j
public class Test {
    public static void main(String[] args) {
        test2();
    }

    private static void test(){
        log.debug("开始运行...");
        Thread t1 = new Thread(() -> {
            log.debug("开始运行...");
            sleep(2);
            log.debug("运行结束...");
        }, "daemon");
        // 设置该线程为守护线程
        t1.setDaemon(true);
        t1.start();
        sleep(1);
        log.debug("运行结束...");
    }

    public static void test2(){
        log.debug("开始运行...");
        CompletableFuture<Void> async = CompletableFuture.runAsync(() -> {
            log.debug("开始运行...");
            sleep(2);
            log.debug("运行结束...");
        });
        sleep(1);
        log.debug("运行结束...");
    }

    private static void sleep(double i) {
        try {
            TimeUnit.MILLISECONDS.sleep((long) i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
